home *** CD-ROM | disk | FTP | other *** search
/ QuickTime 2.0 Developer Kit / QuickTime 2.0 Developer Kit.iso / mac / MAC / Programming Stuff / Documentation / develop / develop Issue 7 / develop 7 code / QuickTime / SimpleMovieOut / SimpleOutSound.c < prev   
Encoding:
C/C++ Source or Header  |  1991-10-15  |  6.7 KB  |  208 lines  |  [TEXT/MPS ]

  1. /************************************
  2. *
  3. * file: SimpleOutSound.c
  4. *
  5. * Program: SimpleOutMovies
  6. * This program creates a movie by spooling in PICTs from a folder
  7. * and adding adding them one at a time to the movie. When all PICTs have
  8. * been processed a sound track is added if one is present.
  9. *
  10. * In this file we have all the routines that allow to add
  11. * a sound track to the movie.
  12. *
  13. * The user is asked to select the file containing the sound.
  14. * The data can be in a 'snd ' resource or in the data fork of the file
  15. * depending in the method used to add the samples.
  16. *
  17. * By Guillermo A. Ortiz 
  18. * April 1991
  19. *
  20. *    Apple Macintosh Developer Technical Support
  21. *
  22. *    Copyright © 1989-1991 Apple Computer, Inc.
  23. *    All rights reserved.
  24. *
  25. ************************************/
  26. #define USEDUMP
  27.  
  28.  
  29.  
  30. #if !defined(USEDUMP)
  31.  
  32. #include <Types.h>
  33. #include <QuickDraw.h>
  34. #include <ToolUtils.h>
  35. #include <Events.h>
  36. #include <Controls.h>
  37. #include <Windows.h>
  38. #include <Dialogs.h>
  39. #include <Menus.h>
  40. #include <Desk.h>
  41. #include <SegLoad.h>
  42. #include <Files.h>
  43. #include <OSEvents.h>
  44. #include <Traps.h>
  45. #include <Fonts.h>
  46. #include <OSUtils.h>
  47. #include <Resources.h>
  48. #include <Memory.h>
  49. #include <Packages.h>
  50.  
  51. #include <Lists.h>
  52. #include <Files.h>
  53. #include <errors.h>
  54. #include <aliases.h>
  55. #include <Movies.h>
  56. #include <MoviesFormat.h>
  57.  
  58. #pragma dump "SimpleOutSound.dump"
  59.  
  60. #else
  61. #pragma load "SimpleOutSound.dump"
  62. #endif
  63.  
  64. /* The following def changes the behavior of the code in the method used
  65.    to add the samples; when defed the data is not moved into the movie
  66.    file but left in the original file. Note that the resulting movie could
  67.    be made a 'normal' movie by calling FlattenMovie on it.
  68. */
  69. // #define __UseExternalFile__
  70.  
  71. /* Again for the sake of simplicity (a good excuse)
  72.    we are assuming here that the 'snd ' resource is format 1 and contains
  73.    only one command with 11Kz sampled sound data. Other resources would have
  74.    to be parsed in a more detailed way in order to get more info out
  75.    of them.
  76. */   
  77.  
  78. #define kTimeScale 11000            /* samples per second */
  79. #define kSoundTrackVolume 0x0100    /* small fixed from -1 to 1 */
  80.  
  81. /* this defs help get the different fields in the 'snd ' resource */
  82. #define kDataOffset        0x2AL        /*  start of real data in 'snd ' */
  83. #define kRateOffset     0x1C        /* offset to rate for samples in 'snd '   */
  84. #define kCountOffset     0x18        /* offset to number of samples in 'snd '  */
  85.  
  86. OSErr AddSoundTrack( Movie, Handle);
  87. extern Handle MyGetSF2(void);
  88. extern OSErr MyGetSF(FSSpec *);
  89.  
  90. void CreateDescriptorFromSnd(Handle, SoundDescriptionHandle);
  91.  
  92. /* Stuff for adding the sample */
  93. TimeValue     sampTime;                        /* Time sample is added to media */
  94. Track        gTrack;
  95. Media        gMedia;
  96.  
  97. OSErr AddSoundTrack( moov, mdrh)
  98. Movie moov;
  99. Handle    mdrh; /* an alias handle for sure */
  100. {
  101.  
  102. short theErr = noErr;
  103. Handle soundDataH;
  104. SoundDescriptionHandle sndDescriptH;
  105. FSSpec   mySpec;
  106. AliasHandle SoundFileAlias;
  107.  
  108. #ifdef __UseExternalFile__
  109. /* When the data is external a ned DatRef record is needed in order indicate the
  110.    container for the data. In the case when we add to the same movie file we keep
  111.    using the reference created when the movie was opened.
  112. */
  113.     if (theErr = MyGetSF(&mySpec) )
  114.       return theErr;
  115.     theErr = NewAlias(nil, &mySpec, &SoundFileAlias);
  116.     DisposHandle(mdrh);
  117. /*** Media data references disappeared, now we use aliases only ***/
  118.     mdrh = (Handle)SoundFileAlias;
  119.     
  120.     if (!theErr ) {
  121. #else
  122.     if ( (soundDataH = MyGetSF2()) ) {            /* the user selected a file and we were able to read the data in */
  123. #endif
  124.  
  125.       /* Now we allocate a handle for the sound descriptor */
  126.       sndDescriptH = (SoundDescriptionHandle) NewHandle(sizeof(SoundDescription));
  127.       if (! sndDescriptH ) /* couldn't get the handle */
  128.         theErr = MemError();
  129.       else {
  130.         CreateDescriptorFromSnd(soundDataH,sndDescriptH);
  131.  
  132. /* From previous file we know that NewMovieTrack changed
  133.         gTrack = NewMovieTrack(moov,(TimeValue) 0,kTimeScale,0,0); */
  134.         gTrack = NewMovieTrack(moov,0,0, kSoundTrackVolume ); /* sound uses no space */
  135.         if (theErr = GetMoviesError()) DebugStr("\pNewMovieTrack Failed");
  136.  
  137. /****** Changed to take aliases directly *******/
  138.         gMedia = NewTrackMedia(gTrack, SOUND_TYPE, kTimeScale, mdrh, 'alis');   
  139.         if (theErr = GetMoviesError()) DebugStr("\pNewTrackMedia Failed");
  140.  
  141. #ifndef __UseExternalFile__
  142.         theErr = BeginMediaEdits( gMedia );        /* We do this since we are adding samples to the media */
  143.         if (theErr) DebugStr("\pBeginMediaEdits Failed");
  144. #endif
  145. #ifdef __UseExternalFile__
  146.         theErr = AddMediaSampleReference(gMedia,0,0x1F780L,
  147.                             (TimeValue)1,(SampleDescriptionHandle) sndDescriptH,0x1F780L, /* I know, I know. You can do it better! */
  148.                             0, &sampTime);
  149.         if (theErr) DebugStr("\pAddMediaSample Failed");
  150. #else
  151.         theErr = AddMediaSample(gMedia,soundDataH,kDataOffset,GetHandleSize(soundDataH),
  152.                             (TimeValue)1,(SampleDescriptionHandle) sndDescriptH,*((long *)((*soundDataH) + kCountOffset)), /* !!!!!!! */
  153.                             0, &sampTime);
  154.         if (theErr) DebugStr("\pAddMediaSample Failed");
  155.  
  156.         theErr = EndMediaEdits( gMedia );                /* We're done adding samples */
  157.         if (theErr) DebugStr("\pEndMediaEdits Failed");
  158.  
  159.         DisposHandle(soundDataH);
  160. #endif
  161. /******* name and parameter changed *****/
  162.         theErr = InsertMediaIntoTrack(gTrack,0L,0L,GetMediaDuration(gMedia), 0x00010000);
  163.         if (theErr) DebugStr("\pInsertTrackMedia Failed");
  164.         DisposHandle((Handle) sndDescriptH);
  165.       }
  166.     }
  167.   return theErr;
  168. }
  169.  
  170. /* This proc fills in the sound description structure necessary to add 
  171.    data to a sound media. 
  172.    
  173. */
  174.  
  175.  
  176. void CreateDescriptorFromSnd(sndRes, sndDescptrH)
  177. Handle sndRes;
  178. SoundDescriptionHandle sndDescptrH;
  179. {
  180. SoundDescriptionPtr sndDescriptP;
  181. Ptr sndResP;
  182.   
  183. #ifndef __UseExternalFile__
  184.   sndResP = *sndRes;
  185. #endif
  186.   sndDescriptP = *sndDescptrH; /* Deref to save some *'s */
  187.  
  188. /**** THESE FIELDS HAVE DIFFERENT NAMES NOW ***/
  189.   sndDescriptP->descSize = sizeof(SoundDescription);                /* total size of SoundDescription including extra data */
  190.   sndDescriptP->dataFormat = 'raw ';        /* my sound comes from a 'snd ' */
  191.   sndDescriptP->resvd1 = 0;                /* reserved */
  192.   sndDescriptP->resvd2 = 0;                /* reserved */
  193.   sndDescriptP->version = 0;            /* which version is this data */
  194.   sndDescriptP->revlevel = 0;            /* what version of that codec did this */
  195.   sndDescriptP->vendor = 0;                /* whose  codec compressed this data */
  196.   sndDescriptP->compressionID = 0;        /* sound compression used, 0 if none */
  197.   sndDescriptP->packetSize = 0;            /* packet size for compression, 0 if no compression */
  198.   sndDescriptP->numChannels = 1;        /* number of channels of sound */
  199.   sndDescriptP->sampleSize = 8;            /* number of bits per sample */
  200. #ifdef __UseExternalFile__
  201.   sndDescriptP->sampleRate = 0x2b7745d1L; /* Yeepee! */
  202.   
  203. #else
  204.   sndDescriptP->sampleRate = 
  205.     *((long *)(sndResP + kRateOffset));    /* sample rate sound is captured at */
  206.     
  207. #endif
  208. }